package top.longmarch.flow.component;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.longmarch.flow.dto.LiteFlowDTO;
import top.longmarch.flow.entity.LiteflowChain;
import top.longmarch.flow.service.ILiteflowChainService;
import top.longmarch.flow.slot.LiteflowChainContext;

import javax.annotation.Resource;

@Slf4j
@Component("flowChainListCmp")
public class FlowChainListCmp extends NodeComponent {

    @Resource
    private ILiteflowChainService liteflowChainService;

    @Override
    public void process() throws Exception {
        LiteFlowDTO req = this.getRequestData();
        LiteflowChainContext context = this.getContextBean(LiteflowChainContext.class);
        LambdaQueryWrapper<LiteflowChain> wrapper = Wrappers.lambdaQuery();
        wrapper.select(LiteflowChain::getId, LiteflowChain::getApplicationName, LiteflowChain::getChainName, LiteflowChain::getChainDesc);
        if (req != null) {
            wrapper.eq(StrUtil.isNotBlank(req.getApplicationName()),
                            LiteflowChain::getApplicationName, req.getApplicationName())
                    .eq(StrUtil.isNotBlank(req.getChainName()),
                            LiteflowChain::getChainName, req.getChainName())
                    .eq(ObjectUtil.isNotNull(req.getEnable()), LiteflowChain::getEnable, req.getEnable());
        }
        context.setLiteflowChainList(liteflowChainService.list(wrapper));
    }

}
